import pathToRegexp from 'path-to-regexp/index';
import { login } from 'services/app';
import { routerRedux } from 'dva/router';
import { config, createUUID } from '../../utils/index';

const {api} = config;
const UUID = createUUID();
console.log(api.user.getAuthCode + UUID);

export default {
    namespace: 'login',
    state: {
        imgcode: api.user.getAuthCode + UUID,
        UUID:UUID,
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {
        },
    },
    effects: {
        // 登录函数
        * loginFun ({ payload }, { call, put }) {
            const data = yield call(login, payload);
            if (data.code === 0) {
                yield put({ type: 'app/updateState', payload: { user: { username: data.data.userName } } });
                localStorage.setItem("userInfor",JSON.stringify(data.data));
                localStorage.setItem("webSocket",'true');
                yield put(routerRedux.push('/home'));

            }
        },
        //获取验证码
        *getcode ({ payload }, { call, put }) {
            const UUIDup = createUUID();
            let imgcode = api.user.getAuthCode+UUIDup;
            yield put({ type: 'save', payload: { imgcode:imgcode,UUID:UUIDup}});
        },
        //输入等于输入
        *inputAndOut ({ payload }, { put }){
            yield put({ type: 'save', payload: payload });
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
